﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-08>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：旧欠托收
//********************************************************************************//
using System;
using System.Web.UI;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui
{
    public partial class JiuQianTS : BasePage
    {
        #region 属性

        /// <summary>
        /// 记录操作人站点
        /// </summary>
        private string zhandianbh
        {
            get
            {
                return Convert.ToString(ViewState["zhandianbh"]);
            }
            set
            {
                ViewState["zhandianbh"] = value;
            }
        }

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 记录列表二查询结果集
        /// </summary>
        private DataTable dtSource2
        {
            get
            {
                return ViewState["dtSource2"] as DataTable;
            }
            set
            {
                ViewState["dtSource2"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 托收明细BLL
        /// </summary>
        YS_SF_TUOSHOUMXBLL _YS_SF_TUOSHOUMXBLL = new YS_SF_TUOSHOUMXBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
            LoadData1(true,null);
            LoadData2(true);
        }

      
        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "createnew")
            {
                DataRowCollection drc = _spHelper.P_YSSF_SHENGCHENGXJZDPD(this.cmbconGuanlik.Value.ToString()).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    cr.IsSuccess = false;
                    cr.Msg = "当前存在一个尚未提交银行的进账单,请在这笔进账单里添加信息！";
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }
                cr.IsSuccess = true;
                e.Result = cr.ToString();
                return;
            }
            else if (e.Parameter == "delmx")
            {
                int shoufeiBH = Convert.ToInt32(this.gvList1.GetRowValues(this.gvList1.FocusedRowIndex, "I_SHOUFEIBH"));
                string tuoshoupzh = Convert.ToString(this.gvList2.GetRowValues(this.gvList2.FocusedRowIndex, "I_TUOSHOUPZH"));
                this._YS_SF_TUOSHOUMXBLL.UpdateYS_SF_TUOSHOUMXesDynamic("I_TICHUBZ=1", "I_TUOSHOUPZH=" + tuoshoupzh + "");
                //更新已托收金额
                string rsNo = "-1";
                string rsMsg = "";
                _spHelper.P_YSSF_UPDATETUOSHOUJE(shoufeiBH, out rsNo, out rsMsg);
                if (rsNo == "1")
                {
                    //更新银行代理汇总明细等信息
                    _spHelper.P_YSSF_GETTUOSHOUSCQK(shoufeiBH, 0, out rsNo, out rsMsg);
                    _spHelper.P_YSSF_GETTUOSHOUSCQK(shoufeiBH, 1, out rsNo, out rsMsg);
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = false;
                    e.Result = cr.ToString();
                }
            }
            else if (e.Parameter == "search")
            {              
                    int index = PageTools.SearchRecordIndex(this.gvList2, this.cmbconGotoType.SelectedIndex == 0 ? "S_YONGHUH" : "I_TUOSHOUPZH", this.txtconYonghuh.Text, true);
                    if (index == -1)
                    {
                        cr.IsSuccess = false;
                        cr.Msg = "当前数据中没有你要找的数据,请检查你输入的条件是否正确。";
                        cr.IsAlertMsg = true;
                    }
                    else
                    {
                        cr.IsSuccess = true;                        
                    }
                    cr.AddValue(index);
                    e.Result = cr.ToString();
            }
        }

        /// <summary>
        /// 执行服务端
        /// </summary>
        protected void btnRunServer_Click(object sender, EventArgs e)
        {
            LoadData1(true,null);
            LoadData2(true);
        }

        /// <summary>
        /// 刷新列表2
        /// </summary>
        protected void gvList2_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData2(true);
        }

        /// <summary>
        /// 刷新列表1
        /// </summary>
        protected void gvList1_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData1(true,null);            
        }

        /// <summary>
        /// 设置背景色等
        /// </summary>
        protected void gvList1_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
        {
            Control c= e.Row.FindControl("aDel");
            if (e.GetValue("I_DAOZHANGBZ").ToString() == "1")
            {
                e.Row.BackColor = System.Drawing.Color.CadetBlue;
                if (c != null)
                {
                    c.Visible = false;
                }
            }
            else if (e.GetValue("I_SHANGCHUANBZ").ToString() == "1")
            {
                e.Row.BackColor = System.Drawing.Color.GreenYellow;
                if (c != null)
                {
                    c.Visible = false;
                }
            }
            
        }


        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            
            PageTools.LoadCombox(this.cmbconGuanlik,PageTools.eLoadType.管理块,BusinessTool.ZHANDIANBH(),null,null);
            this.cmbconGotoType.SelectedIndex = 0;
        }

        /// <summary>
        /// 查询GRIDVIEW1数据
        /// </summary>
        /// <param name="reGet">是否重新读取数据库</param>
        private void LoadData1(bool reGet,string order)
        {
            //加载托收进账单
            if (reGet)
            {
                this.dtSource = _spHelper.P_YSSF_GETJIUQIANTSJZD(this.cmbconGuanlik.Value.ToString(), string.IsNullOrEmpty(order) ? "D_HEZHANGRQ DESC" : order).Tables[0];
            }
            this.gvList1.DataSource = this.dtSource;
            this.gvList1.DataBind();
            if (this.dtSource.Rows.Count == 1)
            {
                this.gvList1.FocusedRowIndex = -1;
                this.gvList1.FocusedRowIndex = 0;
            }
            SetControl();
        }

        /// <summary>
        /// 查询GRIDVIEW2数据
        /// </summary>
        /// <param name="reGet">是否重新读取数据库</param>
        private void LoadData2(bool reGet)
        {
            if (reGet)
            {
                int shoufeiBH = Convert.ToInt32(this.gvList1.GetRowValues(this.gvList1.FocusedRowIndex, "I_SHOUFEIBH"));
                this.hdCollection.Set("sfbh", shoufeiBH);
                if (shoufeiBH != -1)
                {
                    //加载进账单明细
                    this.dtSource2 = _spHelper.P_YSSF_GETTUOSHOUMX(shoufeiBH).Tables[0];
                }
            }
            this.gvList2.DataSource = this.dtSource2;
            this.gvList2.DataBind();
        }

        /// <summary>
        /// 控件明细与新进账单按钮是否可用
        /// </summary>
        private void SetControl()
        {
            //控制增加明细按钮
            //int daozhangbz = Convert.ToInt32(this.gvList1.GetRowValues(this.gvList1.FocusedRowIndex, "I_DAOZHANGBZ"));
            //this.btnCreateNew.ClientEnabled = (daozhangbz != 1);
            //this.btnOpenAddDetail.ClientEnabled = (daozhangbz != 1);
            //将银行接口上传情况设置到hiddenfield
            //int shangchuanbz = Convert.ToInt32(this.gvList1.GetRowValues(this.gvList1.FocusedRowIndex, "I_SHANGCHUANBZ"));
            //this.hdCollection.Set("scbz", shangchuanbz);
        }

        #endregion    
    }
}